// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

package com.brest.managesambafilesharing;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.NetworkInfo;

// Referenced classes of package com.funkyfresh.samba:
//            Installer, SambaControl, LOG, Misc, 
//            WifiWhitelist, SambaWifiInfo, SambaInfo

public class WifiSambaReceiver extends BroadcastReceiver {
	// private int ToastTime = 2;
	//
	// protected OnResponseListenerRunAsRoot onResponseListenerRunAsRoot = new
	// OnResponseListenerRunAsRoot() {
	// public void onResponse(String message) {
	// LOG.d(message);
	// try {
	// // Toast.makeText(context, message, ToastTime).show();
	// new IsSambaRunning(onResponseListenerIsSambaRunning)
	// .execute("status");
	// } catch (Exception e) {
	// LOG.e("onResponse error : " + e.getMessage());
	// }
	// }
	// };
	//
	// protected OnResponseListenerIsSambaRunning
	// onResponseListenerIsSambaRunning = new OnResponseListenerIsSambaRunning()
	// {
	// public void onResponse(String message) {
	// LOG.d(message);
	// try {
	// Toast.makeText(context, message, ToastTime).show();
	// new MyNotificationMgr().createNotification(context, message);
	// } catch (Exception e) {
	// LOG.e("onPostExecute error : " + e.getMessage());
	// }
	// }
	// };
	//
	public WifiSambaReceiver() {
		context = null;
		preferences = null;
		useWifiLock = false;
		useMcastLock = false;
		useNotificationBarIcon = true;
	}

	private boolean startSamba() {
		LOG.d("startSamba()");
		/*
		 * Intent i = new Intent();
		 * i.setClassName("com.brest.managesambafilesharing",
		 * "com.brest.managesambafilesharing.MainActivity");
		 * i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 * i.setAction("INTENTINFO_START"); context.startActivity(i);
		 */
		// String commands[] = { SambaCmdToolBean.STARTSAMBA };
		// new RunAsRoot().start(commands, onResponseListenerRunAsRoot);
		Intent intent = new Intent(context, DummyActivity.class)
				.setAction("INTENTINFO_START");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
		return true;
	}

	private boolean stopSamba() {
		LOG.d("stopSamba()");
		/*
		 * Intent i = new Intent();
		 * i.setClassName("com.brest.managesambafilesharing",
		 * "com.brest.managesambafilesharing.MainActivity");
		 * i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 * i.setAction("INTENTINFO_STOP"); context.startActivity(i); //
		 * context.startActivity(new Intent(context, )
		 */
		// String commands[] = { SambaCmdToolBean.STOPSAMBA };
		// new RunAsRoot().start(commands, onResponseListenerRunAsRoot);
		Intent intent = new Intent(context, DummyActivity.class)
				.setAction("INTENTINFO_STOP");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);

		return true;
	}

	private void wifiNetChanged(NetworkInfo networkinfo) {
		LOG.i((new StringBuilder("wifiNetChanged() netInfo = "))
				.append(networkinfo).append(" isConnected()=")
				.append(networkinfo.isConnected()).toString());
		if (networkinfo.isConnected()) {
			LOG.i((new StringBuilder("wifiNetChanged(): IP=")).append(ip)
					.toString());
			/*
			 * if (wifiWhitelistEnabled &&
			 * !WifiWhitelist.BssidIsWhitelisted(currentBssid)) LOG.i((new
			 * StringBuilder("Not starting samba - Access Point: "))
			 * .append(currentSsid).append("  BSSID: ") .append(currentBssid)
			 * .append(" not in the Wifi White List").toString());
			 * 
			 * else
			 */
			startSamba();
		} else {
			stopSamba();
		}
	}

	private void wifiStateChanged(int i, int j) {
		LOG.i((new StringBuilder(String.valueOf(i))).append(",").append(j)
				.toString());
		if (i == 1)
			stopSamba();
	}

	public void onReceive(Context pcontext, Intent intent) {
		context = pcontext;
		// preferences = PreferenceManager.getDefaultSharedPreferences(c);
		String s = intent.getAction();
		/*
		 * SWInf = new SambaWifiInfo(c); sambaIsEnabled = (new
		 * SambaInfo(c)).sambaIsEnabled(); useWifiLock =
		 * preferences.getBoolean("WifiWakeLock", false); useMcastLock =
		 * preferences.getBoolean("MulticastEnableLock", false);
		 * useNotificationBarIcon =
		 * preferences.getBoolean("NotificationBarIcon", true);
		 * wifiWhitelistEnabled = preferences.getBoolean("WifiWhitelistEnabled",
		 * false); whiteListAllowManualStart = preferences.getBoolean(
		 * "WhiteListAllowManualStart", true); wifiIsUp = SWInf.wifiIsUp();
		 * managedWifiIsUp = SWInf.managedWifiIsUp(); ip = SWInf.getWifiIp();
		 * currentBssid = SWInf.getWifiBssid(); currentSsid =
		 * SWInf.getWifiSsid();
		 */
		LOG.i((new StringBuilder(String.valueOf(Misc.getDateTime())))
				.append(" - Received: :").append(s).append(". ").toString());
		/*
		 * if (s.equals("com.funkyfresh.samba.STOPSAMBA") ||
		 * s.equals("android.intent.action.MEDIA_UNMOUNTED")) { if
		 * (SambaInfo.sambaIsRunning()) stopSamba();
		 * LOG.d("funkyFreshSambaReceiver: returning from onReceive()"); } else
		 * if (!sambaIsEnabled) { LOG.i(
		 * "funkyFreshSambaReceiver: Samba currently disabled: ignoring intents."
		 * ); LOG.d("funkyFreshSambaReceiver: returning from onReceive()"); }
		 * else
		 */
		if (s.equals("android.net.wifi.WIFI_STATE_CHANGED")) {
			wifiStateChanged(intent.getIntExtra("wifi_state", 4),
					intent.getIntExtra("previous_wifi_state", 4));
			LOG.d("managesambafilesharingReceiver: returning from onReceive()");
		} else if (s.equals("android.net.wifi.STATE_CHANGE")) {
			wifiNetChanged((NetworkInfo) intent
					.getParcelableExtra("networkInfo"));
			LOG.d("managesambafilesharingReceiver: returning from onReceive()");
			/*
			 * } else if ((s.equals("com.funkyfresh.samba.RESTARTSAMBA") || s
			 * .equals("android.intent.action.MEDIA_MOUNTED")) && wifiIsUp) { if
			 * (managedWifiIsUp && wifiWhitelistEnabled &&
			 * !whiteListAllowManualStart &&
			 * !WifiWhitelist.BssidIsWhitelisted(currentBssid)) { LOG.i((new
			 * StringBuilder("Not starting samba - Access Point: "))
			 * .append(currentSsid) .append("  BSSID: ") .append(currentBssid)
			 * .append
			 * (" not in the Wifi White List, and AllowManualStart setting=")
			 * .append(whiteListAllowManualStart).toString()); } else {
			 * startSamba();
			 * LOG.d("funkyFreshSambaReceiver: returning from onReceive()"); }
			 */
		} else {
			LOG.d("managesambafilesharingReceiver: ** returning from onReceive()");
		}
	}

	public static final String INTENT_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
	public static final String INTENT_MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
	public static final String INTENT_RESTARTSAMBA = "com.funkyfresh.samba.RESTARTSAMBA";
	public static final String INTENT_STOPSAMBA = "com.funkyfresh.samba.STOPSAMBA";
	private SambaWifiInfo SWInf;
	public Context context;
	private String currentBssid;
	private String currentSsid;
	private String ip;
	private boolean managedWifiIsUp;
	public SharedPreferences preferences;
	private boolean sambaIsEnabled;
	private boolean useMcastLock;
	private boolean useNotificationBarIcon;
	private boolean useWifiLock;
	private boolean whiteListAllowManualStart;
	private boolean wifiIsUp;
	private boolean wifiWhitelistEnabled;
}
